import json

with open('./数据结构与算法/梁智文/student.json', 'r',encoding='utf-8') as f:

    data= json.load(f)
    
students = data["studentData"]

# print(students)


# 快速排序
def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]['name']
    less, equal, greater = [], [], []
    for student in arr:
        if student['name'] < pivot:
            less.append(student)
        elif student['name'] == pivot:
            equal.append(student)
        else:
            greater.append(student)
    return quick_sort(less) + equal + quick_sort(greater)

# 归并排序
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)

def merge(left, right):
    result = []
    while left and right:
        if left[0]['name'] <= right[0]['name']:
            result.append(left.pop(0))
        else:
            result.append(right.pop(0))
    return result + left + right

# 其他排序算法可以类似地实现

# 插入操作
def insert_student(student):
    students.append(student)

# 删除操作
def delete_student(name):
    for student in students:
        if student['name'] == name:
            students.remove(student)
            break

# 模糊查找
def search_student(name):
    result = []
    for student in students:
        if name in student['name']:
            result.append(student)
    return result

# 统计每个省份的人数并按升序打印结果
def count_students_by_province():
    province_count = {}
    for student in students:
        province = student['provice']
        province_count[province] = province_count.get(province, 0) + 1
    sorted_province_count = sorted(province_count.items(), key=lambda x: x[1])
    return sorted_province_count

# 打印排序结果
print("快速排序结果：")
print(quick_sort(students))
print("\n归并排序结果：")
print(merge_sort(students))

# 插入一个新学生并打印
new_student = {
    "id": "5C91096A-EE81-401C-9E7A-52C05E44E111",
    "orderCode": 11,
    "name": "王小明",
    "birthDay": "2002-10-01",
    "gender": True,
    "provice": "广西",
    "techClass": "软件2020级2班"
}
insert_student(new_student)
print("\n插入新学生后的列表：")
print(students)

# 删除一个学生并打印
delete_student("张卫东")
print("\n删除学生后的列表：")
print(students)

# 模糊查找并打印结果
print("\n模糊查找结果：")
print(search_student("张"))

# 统计每个省份的人数并按升序打印结果
print("\n按省份统计人数结果：")
print(count_students_by_province())